//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ia\fn_convergeUnits.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

// [[(getPos _object),(_zone_size / 2)], _owner_side, _share_dist,["Man"]] call FFUNC(convergeUnits)
private ["_position","_side","_radius","_types","_nbr_grps","_entities","_grps","_wp","_grp","_zone","_side_count","_x"];
DEFAULT_PARAM(_position,0,[]);
DEFAULT_PARAM(_side,1,GVAR(side_enemy));
DEFAULT_PARAM(_radius,2,300);
DEFAULT_PARAM(_types,3,ARR_2("Man","LandVehicle"));
DEFAULT_PARAM(_nbr_grps,4,3);

if (!isnil "KRON_UPS_sharedist") then { if (KRON_UPS_sharedist > _radius)  then { _radius = KRON_UPS_sharedist; }; };

if (typeName _types != "ARRAY") then {_types = ["Man","LandVehicle"]};
if (count _types <= 0) then  {_types = ["Man","LandVehicle"]};
if (_nbr_grps <= 0) then { _nbr_grps = 1 ;};

_zone = 30;
if (typeName (_position select 0) == "ARRAY") then {
	_zone = _position select 1;
	_position = _position select 0;
};

if (typename _side == typename []) then {
	for [{_i = 0},{_i < count _side},{_i = _i + 1}] do {
		if (typename (_side select _i) == typename "") then {
			_side set [_i, (switch {toupper((_side select _i))} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_enemy)};	})];
		};
	};
} else {
	if (typename _side == typename "") then {
		_side = switch {toupper(_side)} do { case "EAST": {east}; case "WEST": {west}; case "GUER": {resistance};	case "CIV": {civilian};	default{ GVAR(side_enemy)};	};
	};
	_side = [_side];
};

// if (typeName _side == "STRING") then {
	// _side = switch {toUpper(_side)} do {
		// case "WEST":{west};
		// case "EAST":{east};
		// case "GUER":{resistance};
		// case "CIV":{civilian};
		// default {GVAR(side_enemy)};
	// };
// };

_entities = _position nearEntities [_types, _radius];
_grps = [];
_side_count = 0;
{ _side_count = _side_count + (_x countSide _entities);
} foreach _side;
TRACE_1("Sides",ARR_3(_side,count _entities,_side_count));
if ((_side_count) > 0) then {
	{
		TRACE_1("Units",ARR_4(_x,side _x,group _x,_grps));
		if ((side _x) in _side) then {
			_grp = group _x;
			if (!(_grp in _grps)) then {
				_wp = [_grp, (currentWaypoint _grp)];
				TRACE_1("Group",ARR_3(_grp,_wp,(waypointType _wp)));
				if ((waypointType _wp) in ["","MOVE","SAD","DESTROY"]) then {
					_wp = _grp addWaypoint [_position, _zone];
					_wp setWaypointType "SAD";
					_wp setWaypointSpeed "FULL";
					_wp setWaypointCompletionRadius _zone;
					_grp setBehaviour "COMBAT";
					_grp setCurrentWaypoint _wp;
					_grps set [count _grps, _grp];
					_wp = [_grp, (currentWaypoint _grp)];
					(leader _grp) doMove _position;
					TRACE_1("Group",ARR_3(_grp, _wp, waypointType _wp));
					sleep 0.5;
				};
			};
		};
		if (count _grps >= _nbr_grps) exitwith{};
	} foreach _entities;
};
TRACE_2("Converging",(count _entities), _grps);

TRACE("End");
TMFPROFILERSTOP;
(count _grps);